package superman.app.gr100.biz.gr_expapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

import superman.app.gr100.biz.gr_expapp.R;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * Created by Scor on 2015/12/2.
 */
public class Login extends AppCompatActivity {
    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button submit;
    private final int NULL_HTTP_RESQUEST = 0;
    private final int HTTP_RESQUEST = 1;
    private JSONObject jsonObject;
    @Override
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        usernameEditText = (EditText) findViewById(R.id.username);
        passwordEditText = (EditText) findViewById(R.id.password);
        autoLogin();
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String urlStr = "http://192.168.1.96:3000/user/center/rest";
                int FLAG = HTTP_RESQUEST;
                String method = "POST";
                String paramStr = "username=" + String.valueOf(usernameEditText.getText()) + "&password=" + String.valueOf(passwordEditText.getText());
                httprequest(urlStr, FLAG, method, paramStr);
            }
        });
    }
    private Handler handler = new Handler() {
        public void handleMessage(Message message){
            switch (message.what){
                case HTTP_RESQUEST:
                    jsonObject =  JSONObject.parseObject(String.valueOf(message.obj));
                    SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                    if(jsonObject.getBoolean("result")){
                        editor.putString("username",jsonObject.getString("username"));
                        editor.putString("password",jsonObject.getString("password"));
                        editor.putString("area",jsonObject.getString("area").toString());
                        editor.putString("areaId",jsonObject.getString("areaId").toString());
                        editor.commit();
                        Intent intent = new Intent(Login.this,MainActivity.class);
                        startActivity(intent);
                    }else {
                        editor.clear();
                        Toast.makeText(Login.this,"账号密码错误",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case NULL_HTTP_RESQUEST:
                    Log.d("scor","no more");
                    break;
                default:
                    break;
            }
        }
    };
    private void autoLogin(){
        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
        String username = preferences.getString("username","");
        String password = preferences.getString("password","");
        usernameEditText.setText(username);
        passwordEditText.setText(password);
        if(username == ""||password == ""){

        }else {
            String urlStr = "http://192.168.1.96:3000/user/center/rest";
            int FLAG = HTTP_RESQUEST;
            String method = "POST";
            String paramStr = "username=" + username + "&password=" + password;
            Log.d("scor",username+paramStr+"");
            httprequest(urlStr, FLAG, method, paramStr);
        }
    }
    public void  httprequest(final String urlStr, final int FLAG,final String method,final String paramsStr){
        new Thread(new Runnable(){
            @Override
            public void run() {
                HttpURLConnection connection;
                try{
                    URL url =new URL(urlStr);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(8000);
                    if(method == "POST"){
                        connection.setRequestMethod("POST");
                        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
                        outputStream.writeBytes(paramsStr);
                    }else {
                        connection.setRequestMethod("GET");
                        connection.setReadTimeout(8000);
                    }
                    InputStream inputStream = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder respone = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null){
                        respone.append(line);
                    }
                    Message message = new Message();
                    message.what = FLAG;
                    message.obj = String.valueOf(respone.toString());
                    handler.sendMessage(message);
                }catch(Exception e){
                }
            }
        }).start();
    }
}
